//
//  InfomationController.swift
//  BaoKanSwift
//
//  Created by uncle-R on 16/6/8.
//  Copyright © 2016年 uncle-R. All rights reserved.
//

import UIKit
import SDCycleScrollView

class InfomationController: BBBaseViewController,BBLabelDelegate,UIScrollViewDelegate {
    
 
    var topScrollView : UIScrollView?
    var contentView :UIScrollView?

     // MARK: - 生命周期
    override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
        self.navigationController?.navigationBar.hidden = true
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        createView()
        loadInfo()
    
    }
     // MARK: - 创建控件
    func createView(){
       
        topScrollView = UIScrollView(frame: CGRectMake(0,0,UI_SCREEN_WIDTH,60))
        topScrollView!.backgroundColor = UIColor.init(red: 212/255.0, green: 61/255.0, blue: 62/255.0, alpha: 1.0)
        topScrollView?.contentSize = CGSizeMake(CGFloat(selectedArr.count) * self.width + (CGFloat( selectedArr.count) - CGFloat(1)) * offset, 60)
       
        topScrollView!.bounces = false
        topScrollView?.scrollEnabled = true
        topScrollView!.showsVerticalScrollIndicator = false
        topScrollView!.showsHorizontalScrollIndicator = false

        topScrollView!.delegate = self
        view.addSubview(topScrollView!)
    
        let contentView = UIScrollView()
        contentView.delegate = self
        self.contentView = contentView
        contentView.bounces = false
        contentView.frame = CGRectMake(0, topScrollView!.bottom, UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT - 60 - 44)
        contentView.backgroundColor = UIColor.whiteColor()
        view.addSubview(contentView)
        
    }
    
    func loadInfo(){
        
        for i in 0..<selectedArr.count {
            
            let label = BBLabel()
            label.text = selectedArr[i]["classname"]
            label.frame = CGRectMake(offset + self.width * CGFloat(i), topScrollView!.size.height/2 - height/2+5, width, height)
            label.delegate = self
            label.tag = i + 10000 ///(1-1000tag 值  apple有可能自用)
            if i == 0 {
                label.didSelected = true
            }
            topScrollView!.addSubview(label)
            
        }
        
        for i in 0..<selectedArr.count {
        
            let newsVC = BBNewsController()
            self.addChildViewController(newsVC)
    
        //设置默认控制器.
            if i == 0 {
              newsVC.classID = Int(selectedArr[i]["classid"]!)
                newsVC.view.frame = CGRectMake(0, 0, UI_SCREEN_WIDTH, contentView!.size.height)
                contentView!.addSubview(newsVC.view)
            }
         }
        
        //设置滚动范围
        contentView!.contentSize = CGSizeMake(CGFloat(childViewControllers.count) * UI_SCREEN_WIDTH,contentView!.size.height)
        contentView!.pagingEnabled = true
        contentView!.setContentOffset(CGPointMake(0, 0), animated: true)
    }
    
    //数组.
    let selectedArr :[[String : String]] = [
        [
            "classid" : "10000",
            "classname" : "今日头条"
        ],
        [
            "classid" : "2",
            "classname": "网文快讯"
        ],
        [
            "classid" : "21",
            "classname": "媒体视角"
        ],
        [
            "classid" : "12",
            "classname": "网文IP"
        ],
        [
            "classid" : "264",
            "classname": "企业资讯"
        ],
        [
            "classid" : "33",
            "classname": "作家风采"
        ],
        [
            "classid" : "34",
            "classname": "维权在线"
        ],
        [
            "classid" : "212",
            "classname": "业者动态"
        ],
        [
            "classid" : "132",
            "classname": "风花雪月"
        ],
        [
            "classid" : "396",
            "classname": "独家报道"
        ],
        [
            "classid" : "119",
            "classname": "求职招聘"
        ],
        ]
    
    let  optionalArray = [
        [
            "classid" : "32",
            "classname": "高端访谈"
        ],
        [
            "classid" : "102",
            "classname": "政策解读"
        ],
        [
            "classid" : "111",
            "classname": "写作指导"
        ],
        [
            "classid" : "115",
            "classname": "征稿信息"
        ],
        [
            "classid" : "51",
            "classname": "精彩活动"
        ],
        [
            "classid" : "440",
            "classname": "写作常识"
        ],
        [
            "classid" : "209",
            "classname": "数据分析"
        ],
        [
            "classid" : "208",
            "classname": "统计图表"
        ],
        [
            "classid" : "405",
            "classname": "名家专栏"
        ],
        [
            "classid" : "394",
            "classname": "传统文学"
        ],
        [
            "classid" : "414",
            "classname": "写作素材"
        ],
        [
            "classid" : "281",
            "classname": "游戏世界"
        ],
        [
            "classid" : "57",
            "classname": "娱乐八卦"
        ],
        [
            "classid" : "58",
            "classname": "社会杂谈"
        ],
        [
            "classid" : "56",
            "classname": "影视动画"
        ]
    ]

}


//MARK:    ------------------------- 协议.分类.-----------------------
extension InfomationController{
    /*
     计算类型属性
     */
    var width :CGFloat {
        get {
            return 70
        }
        set(newValue){
            self.width = newValue
        }
        
    }
    var height :CGFloat {
        get {
            return 40
        }
        set(newValue){
            self.height = newValue
        }
    }
    var top :CGFloat {
        set (newValue){
            self.top = newValue
        }
        get {
        return 10
        
        }
    }
    var offset :CGFloat {
        set (newValue){
            self.offset = newValue
        }
        get {
            return 10
            
        }
    }

     // MARK: - BBLabel's Delegate
    
    func labelClick(label: BBLabel, tag: Int) {

 print("调用了")
        
        var offsetX = label.center.x -  CGFloat(UI_SCREEN_WIDTH) / 2
        let offsetMax = (topScrollView?.contentSize.width)! - UI_SCREEN_WIDTH
        
        if offsetX < 0 {
            offsetX = 0
        }
        
       else if offsetX > offsetMax {
            offsetX = offsetMax
        }
    
        topScrollView?.setContentOffset(CGPointMake(offsetX, (topScrollView?.contentOffset.y)!), animated: true)
        
        contentView!.setContentOffset(CGPointMake((CGFloat(tag)-10000) * UI_SCREEN_WIDTH, 0), animated: true)
        
        /* 恢复其他Label
         */
        for i in 0..<selectedArr.count {
//            
            if i != tag - 10000  {
                if let label = topScrollView!.viewWithTag(i+10000) as? BBLabel{
                print("asdasd\(label)")
                label.didSelected = false
                }
            }else{
                label.didSelected = true
            }
    }
  }
    /**
     代码调用  结束滚动调用
     */
    func scrollViewDidEndScrollingAnimation(scrollView: UIScrollView) {
        
       let index = Int(scrollView.contentOffset.x / UI_SCREEN_WIDTH)
       let tempNewsVC = self.childViewControllers[index] as! BBNewsController
        tempNewsVC.classID = Int(selectedArr[index]["classid"]!)
      
        if tempNewsVC.view.superview != nil{ //如果已经展示.不作操作
            return
        }
        tempNewsVC.classID = Int(selectedArr[index]["classid"]!)
        tempNewsVC.view.frame = CGRectMake(CGFloat(index) * UI_SCREEN_WIDTH , 0, UI_SCREEN_WIDTH, contentView!.size.height)
        contentView?.addSubview(tempNewsVC.view)
  
    }
    
    /**
     手动调用  结束滚动调用
     */
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        let index = Int(scrollView.contentOffset.x / UI_SCREEN_WIDTH)
        let temLaebl = topScrollView?.viewWithTag(index+10000) as! BBLabel
        temLaebl.labelClick()
        
        let tempNewsVC = self.childViewControllers[index] as! BBNewsController
        if tempNewsVC.view.superview != nil{ //如果已经展示.不作操作
            return
        }
        tempNewsVC.classID = Int(selectedArr[index]["classid"]!)
        tempNewsVC.view.frame = CGRectMake(CGFloat(index) * UI_SCREEN_WIDTH , 0, UI_SCREEN_WIDTH, contentView!.size.height)
         contentView?.addSubview(tempNewsVC.view)

    }

    
    
}



